const axios = require('axios');

function createAxios(proxy, headers) {
    const axiosInstance = axios.create({
        proxy: proxy,
        headers: headers,
        timeout: 1000 * 30
    });
    return axiosInstance
}


function parseProxy(proxyStr) {
    if (proxyStr == null || proxyStr.length == 0) {
        return null
    }
    let proxy = {
        protocol: "http",
    }
    proxyStr = proxyStr.replace("socks5://", "")
    proxyStr = proxyStr.replace("http://", "")
    proxyArr = proxyStr.split(":")
    if (proxyArr.length >= 2) {
        proxy.host = proxyArr[0]
        proxy.port = proxyArr[1]
    } else {
        return null
    }
    if (proxyArr.length >= 4) {
        proxy.auth = {
            username: proxyArr[2],
            password: proxyArr[3],
        }
    }
    return proxy
}

module.exports = {
    createAxios,
    parseProxy,
}